﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FacturIT.Service.Repositories.Company;
using FacturIT.Service.Repositories.userCompany;
using FacturIT.Service.UserCompany;

namespace FacturIT.Service.Company
{
    internal class UserCompanyFactory
    {
        private static UserCompanyFactory instance = null;
        public static UserCompanyFactory Instance
        {
            get
            {
                if (instance == null)
                    instance = new UserCompanyFactory();
                return instance;
            }
        }

        private UserCompanyService UserCompanyService;
        internal UserCompanyService GetUserCompanyService()
        {
            if (UserCompanyService == null)
                UserCompanyService = new UserCompanyService();

            return UserCompanyService;
        }
    }

    internal class UserCompanyService
    {
        private IRepository<BLL.Model.UserCompany.UserCompany, int> repository;
        private IRepository<BLL.Model.UserCompany.UserCompany, int> Repository
        {
            get
            {
                if (repository == null)
                    repository = new UserCompanyRepository();

                return repository;
            }
        }

        internal UserCompany.UserCompanyDTO AddUserToCompany(int owner, int companyid)
        {
            UserCompanyDTO usercompany = Convert(Repository.Add(new BLL.Model.UserCompany.UserCompany()
            {
                UserID = owner,
                CompanyID = companyid
            }));
            return usercompany;
        }

        internal IEnumerable<UserCompanyDTO> ReadMyCompanies(int userid)
        {
            return from usercompany in ((UserCompanyRepository)Repository).ReadMyCompanies(userid) select new UserCompanyDTO(usercompany);
        }

        #region HelperMethods
        private static UserCompanyDTO Convert(BLL.Model.UserCompany.UserCompany usercompany)
        {
            return new UserCompanyDTO(usercompany);
        }
        #endregion

        
    }
    
    
    internal class CompanyFactory
    {
        private static CompanyFactory instance = null;
        public static CompanyFactory Instance
        {
            get
            {
                if (instance == null)
                    instance = new CompanyFactory();
                return instance;
            }
        }

        private CompanyService CompanyService;
        internal CompanyService GetCompanyService()
        {
            if (CompanyService == null)
                CompanyService = new CompanyService();

            return CompanyService;
        } 
    }
    internal class CompanyService
    {
        private IRepository<BLL.Model.Company.Company, int> repository;
        private IRepository<BLL.Model.Company.Company, int> Repository
        {
            get
            {
                if (repository == null)
                    repository = new CompanyRepository();

                return repository;
            }
        }

        internal CompanyDTO Addcompany(string name, string street, string pc, string city, string country, string phone, int? owner)
        {
            CompanyDTO company = Convert(Repository.Add(new BLL.Model.Company.Company()
            {
                Name = name,
                Street = street,
                PostalCode = pc,
                City = city,
                Country = country,
                Phone = phone,
                Owner = owner
            }));
            return company;
        }

        internal CompanyDTO UpdateCompany(string name, string street, string pc, string city, string country, string phone)
        {
            throw new NotImplementedException();
        }

        internal CompanyDTO ReadCompany(int companyID)
        {
            return Convert(((CompanyRepository)Repository).ReadCompany(companyID));
        }

        internal IEnumerable<CompanyDTO> ReadMyOwnedCompanies(int userID)
        {
            return from company in ((CompanyRepository)Repository).ReadMyOwnedCompanies(userID) select new CompanyDTO(company);
        }

        internal IEnumerable<CompanyDTO> ReadAllCompanies()
        {
            return from company in ((CompanyRepository)Repository).ReadAllCompanies() select new CompanyDTO(company);
        }

        internal CompanyDTO ReadCompanyByName(string companyname)
        {
            return Convert(((CompanyRepository)Repository).ReadCompanyByName(companyname));
        }

        #region HelperMethods

        private static CompanyDTO Convert(BLL.Model.Company.Company company)
        {
            return new CompanyDTO(company);
        }
        #endregion







        
    }
}